<template>
  <div>
    <div
      class="search-bar-wrapper"
      :class="{ 'hide-title': !titleVisible, 'hide-shadow': shadowVisible }"
    >
      <transition name="title-move">
        <div class="search-bar-title-wrapper" v-show="titleVisible">
          <div class="title-text-wrapper">
            <span class="title-text title">{{ $t('home.title') }}</span>
          </div>
          <div class="title-icon-shake-wrapper" @click="showFlapCard()">
            <span class="icon-shake icon"></span>
          </div>
        </div>
      </transition>
      <div
        class="title-icon-back-wrapper"
        :class="{ 'hide-title': !titleVisible }"
        @click="back"
      >
        <span class="icon-back icon"></span>
      </div>
      <div
        class="search-bar-input-wrapper"
        :class="{ 'hide-title': !titleVisible }"
      >
        <!-- 占位 -->
        <div
          class="search-bar-blank"
          :class="{ 'hide-title': !titleVisible }"
        ></div>
        <div class="search-bar-input">
          <span class="icon-search icon"></span>
          <input
            type="text"
            class="input focus"
            :placeholder="$t('home.hint')"
            v-model="searchText"
            @click="showHotSearch"
            @keyup.13.exact="search"
          />
        </div>
      </div>
    </div>
    <hot-search-list
      v-show="hotSearchVisible"
      ref="hotSearch"
    ></hot-search-list>
  </div>
</template>
<script>
import { storeHomeMixin } from '../../utils/mixin'
import HotSearchList from './HotSearchList.vue'
export default {
  mixins: [storeHomeMixin],
  data() {
    return {
      searchText: '',
      titleVisible: true,
      shadowVisible: true,
      hotSearchVisible: false
    }
  },
  components: {
    HotSearchList
  },
  watch: {
    offsetY(offsetY) {
      if (offsetY > 0) {
        this.hideTitle()
        this.showShadow()
      } else {
        this.showTitle()
        this.hideShadow()
      }
    },
    hotSearchOffsetY(offsetY) {
      if (offsetY > 0) {
        this.showShadow()
      } else {
        this.hideShadow()
      }
    }
  },
  methods: {
    // 搜索功能
    search() {
      this.$router.push({
        path: '/store/list',
        query: {
          keyword: this.searchText
        }
      })
    },
    // 显示Card
    showFlapCard() {
      this.setFlapCardVisible(true)
    },
    back() {
      if (this.offsetY) {
        this.showShadow()
      } else {
        this.hideShadow()
      }
      if (this.hotSearchVisible) {
        this.hideHotSearch()
      } else {
        this.$router.push('/store/shelf')
      }
    },
    hideHotSearch() {
      this.hotSearchVisible = false
      if (this.offsetY > 0) {
        this.hideTitle()
        this.showShadow()
      } else {
        this.showTitle()
        this.hideShadow()
      }
    },
    showHotSearch() {
      this.hideTitle()
      this.showShadow()
      this.hotSearchVisible = true
      this.$nextTick(() => {
        // 当hotSearch 显示以后再执行
        this.$refs.hotSearch.reset()
      })
      this.$refs.hotSearch.reset()
    },
    // 显示和隐藏标题
    showTitle() {
      this.titleVisible = true
    },
    hideTitle() {
      this.titleVisible = false
    },
    // 显示和隐藏阴影
    hideShadow() {
      this.shadowVisible = true
    },
    showShadow() {
      this.shadowVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/style/global';
.search-bar-wrapper {
  position: relative;
  z-index: 150;
  width: 100%;
  height: px2rem(94);
  box-shadow: 0 px2rem(2) px2rem(2) 0 rgba(0, 0, 0, 0.1);
  &.hide-shadow {
    box-shadow: none;
  }
  &.hide-title {
    height: px2rem(52);
  }
  .search-bar-title-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: px2rem(48);
    .title-text-wrapper {
      display: flex;
      height: 100%;
      justify-content: center;
      align-items: center;
      .title-text {
      }
    }
    .title-icon-shake-wrapper {
      position: absolute;
      top: 0;
      right: 0;
      .icon-shake {
        margin-right: px2rem(10);
      }
    }
  }
  .title-icon-back-wrapper {
    z-index: 200;
    position: absolute;
    top: 0;
    left: 0;
    height: px2rem(42);
    transition: all $animatinoTime $animatinoType;
    .icon-back {
      margin-left: px2rem(10);
    }
    &.hide-title {
      height: px2rem(52);
    }
  }
  .search-bar-input-wrapper {
    display: flex;
    position: absolute;
    left: 0;
    top: px2rem(42);
    width: 100%;
    height: p2rem(52);
    padding: 0 px2rem(15);
    box-sizing: border-box;
    transition: top $animatinoTime $animatinoType;
    .search-bar-blank {
      flex: 0 0 0;
      width: 0;
      transition: all $animatinoTime $animatinoType;
      &.hide-title {
        flex: 0 0 px2rem(31);
        width: px2rem(31);
      }
    }
    &.hide-title {
      top: px2rem(8);
    }
    .search-bar-input {
      width: 100%;
      background: #f4f4f4;
      padding: px2rem(5) px2rem(15);
      border-radius: px2rem(20);
      border: px2rem(1) solid #eee;
      box-sizing: border-box;
      @include left;
      .icon-search {
        color: #999;
      }
      .input {
        width: 100%;
        height: px2rem(22);
        margin-left: px2rem(10);
        font-size: px2rem(12);
        color: #757575;
        box-sizing: border-box;
        border: none;
        background: transparent;
        &::placeholder {
          color: #d1d1d0;
        }
        &.focus {
          outline: none;
        }
      }
    }
  }
}
</style>
